//
//  LRAccountPage.m
//  LRAccount
//
//  Created by dev on 2018/7/30.
//  Copyright © 2018年 武汉伴悦科技. All rights reserved.
//

#import "LRAccountPage.h"
#import "LR_AlertHelper.h"
@interface LRAccountPage()
@property (nonatomic,weak) UIButton *submitBtn;

@property (nonatomic,weak) UITextField *phoneTextField;

@property (nonatomic,weak) UITextField *passwordTextField;

@property (nonatomic,copy) accountPageNetBlock block;
@property (nonatomic,strong) NSArray *controlsArr;
@end
@implementation LRAccountPage
-(void)pageGenerateWithControls:(NSArray *)controlsArr andNetBlock:(accountPageNetBlock)block;
{
    self.controlsArr=controlsArr;
    self.block =block;
    for (id object in controlsArr) {
        if([object isKindOfClass:[LRPageModel class]])
        {
            LRPageModel *model=(LRPageModel *)object;
            LRPageControlType type=model.controlType;
            UIControl *control=model.control;
                switch (type) {
                    case LRPageControlTypeSubmitButtom:
                        {
                            self.submitBtn=(UIButton *)control;
                            if(self.submitBtn!=nil)
                            {
                                [self.submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
                                
                            }
                            
                        }
                        break;
                    case LRPageControlTypePhone:
                    {
                        UITextField *textField=(UITextField *)control;
                        if(textField!=nil)
                        {
                            self.phoneTextField=textField;
                            
                        }
                        
                        
                    }
                        break;
                    case LRPageControlTypePassword:
                    {
                        UITextField *textField=(UITextField *)control;
                        if(textField!=nil)
                        {
                            self.passwordTextField=textField;
                            
                        }
                        
                        
                    }
                        break;

                        
                    default:
                        break;
                }
                
            
            
            
        }
    }
    
}

-(void)submitClick
{
    BOOL allJudge=YES;
    
    NSMutableDictionary * netDic=[[NSMutableDictionary alloc]init];;
    if(self.phoneTextField!=nil)
    {
        
       allJudge= [self phoneJudge:self.phoneTextField];
        [netDic setObject:self.phoneTextField.text forKey:@"phone"];
        if(allJudge==NO)
        {
            
            return;
        }
    }
    if(self.passwordTextField!=nil)
    {
        allJudge=[self passwordJudge:self.passwordTextField];
        [netDic setObject:self.passwordTextField.text forKey:@"password"];
        if(allJudge==NO)
        {
            
            return;
        }
        
    }
    
    
    
    if(allJudge==YES)
    {
        NSDictionary *reDic=[[NSDictionary alloc]initWithDictionary:netDic];
        self.block(reDic);
        
    }
    
}

-(BOOL)phoneJudge:(UITextField *)phoneText
{
    BOOL judge=YES;
    if([phoneText.text isEqualToString:@""])
    {
        [LR_AlertHelper showMessage:@"请输入手机号"];
        judge=NO;
        
    }else
    
    if(![self predicatePhone:phoneText.text])
    {
        [LR_AlertHelper showMessage:@"手机号码不正确"];
        judge=NO;
    }
    else
    {
        
        judge=YES;
    }
    
    return judge;
    
    
}

-(BOOL)passwordJudge:(UITextField *)passwordText
{
    BOOL judge=YES;
    if([passwordText.text isEqualToString:@""])
    {
        [LR_AlertHelper showMessage:@"请输入密码"];
        judge=NO;
        
    }else
        
        if(![self predicatePassword:passwordText.text])
        {
            [LR_AlertHelper showMessage:@"密码位数不正确"];
            judge=NO;
        }
        else
        {
            
            judge=YES;
        }
    
    return judge;
    
    
}

-(BOOL)predicatePhone:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

-(BOOL)predicatePassword:(NSString *)password
{
    BOOL isPas=NO;
    if(password.length<6||password.length>16)
    {
        isPas=NO;
        
    }
    else
    {
        
        isPas=YES;
    }
    
    return isPas;
}


@end
